home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 731 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. From: tony@online.tmx.com.au (Tony Cook)
  2. Message-ID: <199603150208.MAA05447@online.tmx.com.au>
  3. X-Original-Date: Fri, 15 Mar 1996 12:08:35 +1000
  4. Path: in2.uu.net!bounce-back
  5. Date: 15 Mar 96 02:13:29 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Return-Path: <daemon@meeker.UCAR.EDU>
  8. Newsgroups: comp.std.c++
  9. Subject: Re: Problem with new in templates...
  10. Organization: Home
  11. References: <adamnash-1303962301470001@gel.stanford.edu>
  12. X-Newsreader: TIN [version 1.2 PL2]
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMUjSUuEDnX0m9pzZAQExRQF+IndIP3xgdWyRVYlfkQXf/xG4Z50G3Ism
  15.     vVXjZGlyIgkp0z62qDJP0NSavJGArybh
  16.     =QiI2
  17.  
  18. Adam Nash (adamnash@cs.stanford.edu) wrote:
  19. : OK Problem Scenario:
  20.  
  21. : There is a templatized data structure that is based on type DATA.  This
  22. : data structure features a method that depends on a method in type DATA. 
  23. : Thus, for semantic reasons, the template assumes that DATA is a pointer
  24. : type.
  25. :...
  26. : The problem occurs on the second line.  data is of type DATA, so you
  27. : really want to allocate a new (*DATA).  IE, if DATA is a CBar *, then data
  28. : is a CBar *,
  29. : so we want it to say: data = new CBar;
  30.  
  31. : How can I do this, only knowing the pointer type?
  32. : IE, is there a version of new that can take the pointer type, rather than
  33. : the actual type?  RTTI is fair game.
  34.  
  35. How about:
  36.     template <class T>
  37.         T* new_of_ptr(T *  /* ignored */)
  38.     {
  39.       return new T;
  40.     }
  41.  
  42.     ...
  43.     DATA value = new_of_ptr((DATA)0);
  44.     ...
  45.  
  46. -- 
  47.         Tony Cook - tony@online.tmx.com.au
  48.                     100237.3425@compuserve.com
  49. ---
  50. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  51. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  52. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  53. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  54. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  55.